﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Yelper.Phone.ValueConverters {
    public class YelpTimeAgoFormatter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value is DateTime) {
                var date = (DateTime)value;
                var timeAgo = DateTime.UtcNow - date;                
                if (timeAgo.TotalHours > 24) { //meer dan één dag geleden
                    return string.Format("{0:d MMM}", date); //9 mrt
                } else if (timeAgo.TotalHours >= 2) { //meer dan twee uren geleden
                       return string.Format("{0:0} hours", timeAgo.TotalHours); //2 hours                
                } else if (timeAgo.TotalHours >= 1) { //meer dan één uur geleden
                    return string.Format("{0:0} hour", timeAgo.TotalHours); //1 hour
                } else if (timeAgo.TotalMinutes >= 2) { //meer dan twee minuten geleden
                    return string.Format("{0:0} mins", timeAgo.TotalMinutes); //40 mins
                } else if (timeAgo.TotalMinutes >= 1) { //meer dan één minuut geleden
                    return string.Format("{0:0} mins", timeAgo.TotalMinutes); //1 min
                } else if (timeAgo.TotalSeconds >= 2) {
                    return string.Format("{0:0} seconds", timeAgo.TotalSeconds); //3 seconds                
                } else { //minder dan twee seconden geleden
                    return string.Format("{0:0} second", timeAgo.TotalSeconds); //1 second
                }
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}
